package com.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.password.MyMD5;
import com.server.service.ICommonDataService;
import com.server.service.ILoginService;
import com.server.service.IVerifyService;
import com.server.web.SessionUtil;

@Service("LoginService")
public class LoginService implements ILoginService{
	
	@Autowired 
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDateService;
	@Autowired
	private IVerifyService  verifyService;

	@Override
	public Map<String, Object> Login(String name, String password) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Boolean isSuccess = null;
		String nickname = null;
		String uuid = null;
		String info = null;
		Integer id = 0;
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
			info = "账号和密码不能为空！";
			isSuccess = false;
			returnMap.put("isSuccess",isSuccess);
			returnMap.put("info",info);
			returnMap.put("nickname",nickname);
			returnMap.put("uuid",uuid);
			return returnMap;
		}
		//密码3次md5加密
		String encryptStr = MyMD5.circularlyEncrypt(password, 3);
		String sql = "select * from education.user where (nickname='%s' or mobile_phone='%s') and password = '%s'";
		List <Map<String,Object>> list = jdbcTemplate.queryForList(String.format(sql, name,name,encryptStr));
		if(list.size()>0){
			Map<String, Object> map = list.get(0);
			Object valid = map.get("valid");
			if (valid != null && (Boolean)valid) {
				nickname = (String) map.get("nickname");
				uuid = (String) map.get("uuid");
				id = (Integer) map.get("id");
				isSuccess = true;
				info = "登录成功！";
			}else{
				isSuccess = false;
				info = "您的账号出现异常，暂时停用!";
			}
		}else{
			isSuccess = false;
			info = "用户名或密码错误！";
		}
		
		
		returnMap.put("isSuccess",isSuccess);
		returnMap.put("info",info);
		returnMap.put("nickname",nickname);
		returnMap.put("uuid",uuid);
		returnMap.put("id",id);
		return returnMap;
	}

	@Override
	public Map<String, Object> Checkphone(String telephone,String nickname) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String info = null;
		Boolean isSuccess = null;
		if(StringUtils.isEmpty("telephone") || StringUtils.isEmpty("nickname")){
			info = "手机号和昵称不能为空！";
			isSuccess = false;
			returnMap.put("isSuccess", isSuccess);
			returnMap.put("info", info);
			return returnMap;
		}
		String sql = "Select mobile_phone,nickname from education.user where mobile_phone='%s' or nickname='%s'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, telephone,nickname));
		if(list.size()>0){
			if(list.size()>1){
				info = "手机号和昵称已注册！";
				isSuccess = false;
				returnMap.put("isSuccess", isSuccess);
				returnMap.put("info", info);
			}else{
				Map<String, Object> map = list.get(0);
				if(((String)map.get("mobile_phone")).equals(telephone) && ((String)map.get("nickname")).equals(nickname) ){
						info = "手机号和昵称已注册！";
						isSuccess = false;
						returnMap.put("isSuccess", isSuccess);
						returnMap.put("info", info);
				}
				if(((String)map.get("mobile_phone")).equals(telephone) && !((String)map.get("nickname")).equals(nickname) ){
						info = "手机号已注册！";
						isSuccess = false;
						returnMap.put("isSuccess", isSuccess);
						returnMap.put("info", info);
				}
				if(!((String)map.get("mobile_phone")).equals(telephone) && ((String)map.get("nickname")).equals(nickname)){
					info = "昵称已注册！";
					isSuccess = false;
					returnMap.put("isSuccess", isSuccess);
					returnMap.put("info", info);
				}
			}
		}else{
			info = "手机号和昵称可用";
			isSuccess = true;
			returnMap.put("isSuccess", isSuccess);
			returnMap.put("info", info);
		}
		return returnMap;
		
	}

	@Override
	public Map<String, Object> add(Map<String, Object> userMap) {
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = "";
		
		Object phoneObj = userMap.get("telephone");
		Object pswObj = userMap.get("password");
		Object nickNameObj = userMap.get("nickname");
		Object uuidObj = userMap.get("uuid");
		
		//MD5经过3次加密
		String encryptStr = MyMD5.circularlyEncrypt(pswObj.toString(), 3);
		// 添加用户
		String sql = "insert into education.user (mobile_phone,nickname,password,creater,createTime,isVerify,uuid) values ('%s','%s','%s','%s',now(),1,'%s')";
		try {
			jdbcTemplate.execute(String.format(sql, phoneObj,nickNameObj,encryptStr,nickNameObj,uuidObj));
			isSuccess = true;
			info = "注册成功！";
		} catch (Exception e) {
			isSuccess = false;
			info = "注册失败";
		}
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("info", info);
		return returnMap;
	}

	@Override
	public Map<String, Object> fupdatePassword(String telephone, String password) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Boolean isSuccess = false;
		String info = null;
		Map<String, Object> inputMap = new HashMap<String, Object>();
		String encryptStr = MyMD5.circularlyEncrypt(password, 3);
		inputMap.put("password", encryptStr);
		String sql = "select * from education.user where mobile_phone='%s'";
		List <Map<String,Object>> list = jdbcTemplate.queryForList(String.format(sql, telephone));
		if(list.size()>0){
			Map<String, Object> map = list.get(0);
			Boolean state = commonDateService.update(map.get("nickname").toString(), "education.user", "mobile_phone", telephone, inputMap);
			if(state){
				isSuccess = true;
				info = "重置密码成功！";
			}else{
				isSuccess = false;
				info = "重置密码失败！";
			}
		}else{
			isSuccess = false;
			info = "请输入你注册的手机号！";
		}
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("info", info);
		return returnMap;
	}

	@Override
	public Boolean checkPhoneNo(String telephone) {
		Boolean isSuccess = false;
		String sql = "select * from education.user where mobile_phone='%s'";
		List <Map<String,Object>> list = jdbcTemplate.queryForList(String.format(sql, telephone));
		if(list.size()>0){
			isSuccess = true;
		}
		return isSuccess;
	}

	@Override
	public Map<String, Object> getInfo(String uuid) {
		String sql = "select * from education.user where uuid='%s'";
		List<Map<String,Object>> list = jdbcTemplate.queryForList(String.format(sql, uuid));
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
}
